'''
NBUitl - Nokia backup file utilities
Copyright (C) 2010 Xiong Huang

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.

Project homepage: http://code.google.com/p/nbutil/

@author: Xiong Huang, xiong@moblet.cn

Created on Mar 31, 2010

'''
class Contact(dict):
    '''
    Contact object from NBU file
    '''
    def __init__(self):
        '''
        Create a Contact instance
        '''
        pass

    def getName(self):
        '''
        Get contact's name
        '''
        return self.get('N').replace(';', '')

    def getCell(self):
        '''
        Get cell phone number.
        If the contact has multiple cell numbers, return the first one.
        '''
        cells = self.get('TEL;CELL', '')
        if type(cells) == list:
            return cells[0]
        else:
            return cells

    def getCells(self):
        '''
        Get all cell phone numbers, return a list anyway
        '''
        cells = self.get('TEL;CELL', [])
        if type(cells) != list:
            return [cells]
        else:
            return cells

    def vcard(self):
        '''
        Return the vcard string for the contact
        '''
        vcard = 'BEGIN:VCARD\n'
        for key,value in self.items():
            if type(value) == list:
                for v in value:
                    vcard += key + ':' + v + '\n'
            else:
                vcard += key + ':' + value + '\n'
        vcard += 'END:VCARD\n'
        return vcard
